package com.carpedil.smvc.controller;

import com.carpedil.smvc.po.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

/** @author Administrator */
@Controller
public class HelloController {

  /**
   * loginf() 请求转发跳转
   *
   * @return view(xxx.jsp location)
   */
  @RequestMapping("/loginf")
  private String loginf() {
    return "forward:/login.jsp";
  }

  /**
   * loginr() 重定向跳转 (关注地址栏变化)
   *
   * @return view(xxx.jsp location)
   */
  @RequestMapping("/loginr")
  private String loginr() {
    return "redirect:/login.jsp";
  }

  /**
   * login1(User user, HttpSession session) 使用HttpSession对象传值,然后重定向redirect至指定jsp 登录操作(推荐用session传值)
   *
   * @param user user
   * @param session session
   * @return view(xxx.jsp location)
   */
  @RequestMapping("/login1")
  private String login1(User user, HttpSession session) {
    System.out.println(user);
    session.setAttribute("u", user);
    //    //重定向至home.jsp
    //    return "forward:/welcome.jsp";
    // 请求转发至home.jsp
    return "redirect:/welcome.jsp";
  }

  /**
   * login2(User user, HttpServletRequest request) 使用HttpServletRequest
   * request传值,然后请求转发forward至指定jsp页面
   *
   * @param user user
   * @param request request
   * @return view(xxx.jsp location)
   */
  @RequestMapping("/login2")
  private String login2(User user, HttpServletRequest request) throws UnsupportedEncodingException {
    System.out.println(user);
    request.setCharacterEncoding("utf-8");
    request.setAttribute("u", user);
    return "forward:/welcome.jsp";
  }
}
